package com.hdw.lucene;

import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {
	public static void search(String indexDir,String q)throws Exception{
		Directory dir=FSDirectory.open(Paths.get(indexDir));
		IndexReader reader=DirectoryReader.open(dir);
		IndexSearcher is=new IndexSearcher(reader);
		Analyzer analyzer=new StandardAnalyzer();//标准分词器
		QueryParser parser=new QueryParser("content", analyzer);
		Query query=parser.parse("You");
		TopDocs hits=is.search(query, 10);
		System.out.println(hits.totalHits);
		for(ScoreDoc scoreDoc:hits.scoreDocs){
			Document doc= is.doc(scoreDoc.doc);
			System.out.println(doc.get("fullPath"));
		}
		reader.close();
	}
	public static void main(String[] args) {
		String indexDir="D:\\lucene";
		String q="May";
		try {
			search(indexDir,q);
		}
		catch (Exception e) {
			System.out.println("worng");
		}
	}
}
